package com.tiger.web.controller.project;


import com.tiger.common.annotation.Log;
import com.tiger.common.core.controller.BaseController;
import com.tiger.common.core.domain.AjaxResult;
import com.tiger.common.core.domain.model.LoginUser;
import com.tiger.common.core.page.TableDataInfo;
import com.tiger.common.enums.BusinessType;
import com.tiger.common.utils.ServletUtils;
import com.tiger.common.utils.StringUtils;
import com.tiger.common.utils.spring.SpringUtils;
import com.tiger.project.domain.CommonDecision;
import com.tiger.project.service.ICommonDecisionService;
import com.tiger.framework.web.service.TokenService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import io.swagger.annotations.ApiParam;
import org.apache.poi.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * 【决策依据管理】Controller
 *
 * @author standard
 * @date 2022-09-14
 */
@Api(tags = "决策依据管理")
@RestController
@RequestMapping("/system/decision")
public class CommonDecisionController extends BaseController {
    @Autowired
    private ICommonDecisionService commonDecisionService;

    /**
     * 查询【决策依据管理】列表
     */
    @ApiOperation(value = "查询【决策依据管理】列表")
    //@PreAuthorize("@ss.hasPermi('system:decision:list')")
    @GetMapping("/list")
    public TableDataInfo list(CommonDecision commonDecision) {
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
        commonDecision.setCompanyId(loginUser.getUser().getDeptId());
        startPage();
        List<CommonDecision> list = commonDecisionService.selectCommonDecisionList(commonDecision);
        return getDataTable(list);
    }


    /**
     * 获取【决策依据管理】详细信息
     */
    @ApiOperation(value = "获取【决策依据管理】列详细信息")
    //@PreAuthorize("@ss.hasPermi('system:decision:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(commonDecisionService.selectCommonDecisionById(id));
    }

    /**
     * 新增【决策依据管理】
     */
    @ApiOperation(value = "新增【决策依据管理】")
    //@PreAuthorize("@ss.hasPermi('system:decision:add')")
//    @Log(title = "新增【决策依据管理】", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody CommonDecision commonDecision) {
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
        commonDecision.setCompanyId(loginUser.getUser().getDeptId());
        commonDecision.setUploadId(loginUser.getUserId());
        commonDecision.setUploadName(loginUser.getUsername());
        return toAjax(commonDecisionService.insertCommonDecision(commonDecision));
    }

    /**
     * 修改【决策依据管理】
     */
    @ApiOperation(value = "修改【决策依据管理】")
    //@PreAuthorize("@ss.hasPermi('system:decision:edit')")
//    @Log(title = " 修改【决策依据管理】", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody CommonDecision commonDecision) {
        return toAjax(commonDecisionService.updateCommonDecision(commonDecision));
    }

    /**
     * 删除【决策依据管理】
     */
    @ApiOperation(value = "删除【决策依据管理】")
    //@PreAuthorize("@ss.hasPermi('system:decision:remove')")
//    @Log(title = "删除【决策依据管理】", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(commonDecisionService.deleteCommonDecisionByIds(ids));
    }



    @ApiOperation(value = "子集决策依据详情分页")
    @GetMapping("/info")
    public TableDataInfo info(String decisionBasisIds, @ApiParam(value = "页面大小") int pageSize, @ApiParam(value = "当前页面") int currentPage) {
        ArrayList<CommonDecision> arrayList = new ArrayList<>();
        if (StringUtils.isNotEmpty(decisionBasisIds)){
            List<String> ids = Arrays.asList(decisionBasisIds.split(","));
            for (String id : ids) {
                CommonDecision decision = commonDecisionService.selectCommonDecisionById(Long.valueOf(id));
                arrayList.add(decision);
            }
        }

//        startPage();
        List<CommonDecision> currentPageList = new ArrayList<>();
        if (arrayList != null && arrayList.size() > 0) {
            int currIdx = (currentPage > 1 ? (currentPage - 1) * pageSize : 0);
            for (int i = 0; i < pageSize && i < arrayList.size() - currIdx; i++) {
                CommonDecision decision1 = arrayList.get(currIdx + i);
                currentPageList.add(decision1);
            }
        }
        return getDataTable(currentPageList);
    }
}
